<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.jmzh1688.com/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// | jmzhfsauto
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\HomeModel;
class Home extends Base
{
    public function getWhereFromPara()
    {
        $param = input('param.');
        $where=[];
        if(array_key_exists('',$param)) {$where['']=$param[''];}  
        return $where;
    }
    public function _initialize()
    {
    	parent::_initialize();
        $wherep=$this->getWhereFromPara();
        $furl=url('home/index' ,$wherep);
        $faddurl=url('home/add' ,$wherep);
        $fediturl=url('home/edit' ,$wherep);
        $homemodel = new HomeModel();
        $tabblename = $homemodel->getTable();//获取表
       $param = input('param.');
       if(array_key_exists('goto',$param)) {$fediturl=$fediturl."?goto=".$param["goto"];}  
        $this->assign([
            'nowurl' => $furl,
            'addurl' => $faddurl,
            'editurl'=> $fediturl,
            'tabblename' => $tabblename
        ]);
    }
    public function getData()
    {
          $wherep=$this->getWhereFromPara();
    	  $param = input('param.');
    	    if(array_key_exists('pageSize',$param))
    	    {
    	      $limit = $param['pageSize'];
              $offset = ($param['pageNumber'] - 1) * $limit;	
    	    }
    	    else {
    	     $limit=100000;
    	     $offset=0;
    	    }
            $homemodel = new HomeModel();
            $where = getWhereForm( $param);
            $where['isdelete']=0;
            $whereor=[];
            $where=array_merge($where,$wherep);
            $list = $homemodel->getByWhere($where, $offset, $limit, $whereor);
            foreach($list as $key=>$vo){
                $list[$key]['operate'] = $this->getRowButton($vo,$wherep);
            }
            $result['total'] =$homemodel->getAllCount($where, $whereor);  // 总数据
            $result['rows'] = $list;
          return $result;
    }
    public function index()
    {
        $wherep=$this->getWhereFromPara();
        if(request()->isAjax()){
        	$result=$this->getData();
           return json($result);
        }
        $toolbarshow= $this->getToolBar($wherep);
        $this->assign([
            'toolbar' => $toolbarshow
        ]);
        return $this->fetch();
    }
    public function del()
    {
        $homemodel = new HomeModel();
        if(request()->isPost()){
            $id = input("post.id");
            $msg = $homemodel->delById($id);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
        return $this->fetch();
    }
     public function view()
    {
        $homemodel = new HomeModel();
        if(request()->isPost()){
            $param = input("post.");
            foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
            $wherep=$this->getWhereFromPara();
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }
            if(session('roleid')>5){
                $param["ispublic"]=0;
            }
            $msg = $homemodel->edit($param);
            return json(msg($msg['code'], $msg['data']."?". $parastr, $msg['msg']));
        }
        $id = input('param.id');
        $info=$homemodel->getById($id);
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch();
    }
    public function edit()
    {
        $homemodel = new HomeModel();
        if(request()->isPost()){
            $param = input("post.");
            foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
            $wherep=$this->getWhereFromPara();
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }
            if(session('roleid')>5){
                $param["ispublic"]=0;
            }
            $msg = $homemodel->edit($param);
            $gotourl=$msg['data']."?". $parastr;
            // 检查是否存在某个具体的参数，所有修改页都跳转到来源页。
             $param2 = input("get.");
           if (isset($param2['goto'])&& $param2['goto']=="self") {
               $gotourl=$_SERVER['HTTP_REFERER'];
           }
            return json(msg($msg['code'],$gotourl , $msg['msg']));
        }
        $id = input('param.id');
        $info=$homemodel->getById($id);
        if(empty($info))//单个扩展表,则取对应的扩展信息表
        {
            $where=$this->getWhereFromPara();
            $where["isdelete"]=0;
            $info=Db::name("my_home")->where($where)->find();
            if(empty($info))
            {
                 $mydata=$this->getWhereFromPara();
                 $mydata["create_time"]=time();
                 $mydata["isdelete"]=0;
                    //加载JSON默认配置项
                 $homemodel->add($mydata);
                 $info=Db::name("my_home")->where($where)->find();
            }
        }
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch();
    }
    public function add_test()
    {
        if(request()->isPost()){
            $homemodel = new HomeModel();
            $param = input("post.");
            $wherep=$this->getWhereFromPara();
            $param=array_merge($param,$wherep);
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }
			foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
            if(session('roleid')>5){
                $param["ispublic"]=0;
            }
            $msg = $homemodel->add($param);
            return json(msg($msg['code'], $msg['data']."?". $parastr, $msg['msg']));
        }
        return $this->fetch();
    }
    public function add()
    {
        if(request()->isPost()){
            $homemodel = new HomeModel();
            $param = input("post.");
            $wherep=$this->getWhereFromPara();
            $param=array_merge($param,$wherep);
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }
			foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
            if(session('roleid')>5){
                $param["ispublic"]=0;
            }
            $msg = $homemodel->add($param);  
            return json(msg($msg['code'], $msg['data']."?". $parastr, $msg['msg']));
        }
        return $this->fetch();
    }
        private function getRowButton($vo,$wherep)
        {
            $id=$vo['id'];
            $idwhere=$wherep;
            $idwhere['id']=$id;
            $operatestr='[{"uuid":"edit","title":"编辑","href":"'.url('edit', ['id' => $id]).'","icon":"","style":"","para":"","isblank":"0","isnouse":0},{"uuid":"view","title":"查看","href":"'.url('view', ['id' => $id]).'","icon":"","style":"","para":"","isblank":"0","isnouse":0},{"uuid":"del","title":"删除","href":"javascript:del(this,$id)","icon":"","style":"","para":"","isblank":"0","isnouse":0}]';
            if(is_array($vo))
            {
            	  $arr=$vo;
            }
            else
            {
              $arr=$vo->toArray();	
            }
            foreach ($arr as $key => $value)
            {
                $operatestr=str_replace("$".$key,$value, $operatestr);
            }
               $operate=json_decode($operatestr,true);
              $option= getButtonHtml($operate);
                return $option;
        }
        private function getToolBar($where)
        {
            $operatestr='[{"uuid":"add","title":"添加","href":"'.url('add' ,$where).'","suffix":"","icon":"","style":"btn btn-outline-primary btn-sm ","para2":"","para1":"0","para":"","isblank":"0","isnouse":0}]';
               foreach ($where as $key => $value)
               {
                   $operatestr=str_replace("$".$key,$value, $operatestr);
               }
               $operate=json_decode($operatestr,true);
              $option= getButtonHtml($operate);
                return $option;
        }
        public function getjsonview($v)
        {
            return $this->fetch($v);
        }
      /*contentadd*/
}
